package com.domain.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JOptionPane;

import com.views.AdminView;
import com.views.CreateUserView;
import com.views.ForgotPasswordView;
import com.views.HomeView;
import com.views.LoginView;

public class HomeViewController {

	private HomeView homeView;
	private ActionListener actionListener;

	public HomeViewController(HomeView homeView) {

		this.homeView = homeView;
	}

	public void controll() {

		actionListener = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				Object obj = e.getSource();

				if (obj.equals(homeView.getCreateUser())) {

					CreateUserView createUserView = new CreateUserView();
					homeView.getDesktopPane().add(createUserView);

					CreateUserController createUserController = new CreateUserController(
							createUserView);

					createUserController.controll();
				}
				if (obj.equals(homeView.getLogin())) {

					LoginView loginView = new LoginView();
					homeView.getDesktopPane().add(loginView);

					LoginController loginController = new LoginController(
							loginView);

					loginController.controll();

				}

				if (obj.equals(homeView.getExit())) {

					int option = JOptionPane.showConfirmDialog(null,
							"Are you sure you want to exit?",
							"confirm dialogue",
							JOptionPane.YES_NO_CANCEL_OPTION);

					if (option == 0) {

						System.exit(0);
					}
				}

				if (obj.equals(homeView.getAdminLogin())) {

					AdminView adminView = new AdminView();
					homeView.getDesktopPane().add(adminView);

					AdminController adminController = new AdminController(
							adminView);
					adminController.controll();

				}
				if (obj.equals(homeView.getForgotPassword())) {

					ForgotPasswordView forgotPasswordView = new ForgotPasswordView();
					homeView.getDesktopPane().add(forgotPasswordView);

					ForgotPasswordController forgotPasswordController = new ForgotPasswordController(
							forgotPasswordView);
					forgotPasswordController.controll();

				}

				if (obj.equals(homeView.getCalculator())) {

					try {
						Runtime.getRuntime().exec("calc");
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
				if (obj.equals(homeView.getNotepad())) {
					try {
						Runtime.getRuntime().exec("notepad");
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}

			}
		};

		homeView.getCreateUser().addActionListener(actionListener);
		homeView.getLogin().addActionListener(actionListener);
		homeView.getExit().addActionListener(actionListener);
		homeView.getCalculator().addActionListener(actionListener);
		homeView.getNotepad().addActionListener(actionListener);
		homeView.getForgotPassword().addActionListener(actionListener);
		homeView.getAdminLogin().addActionListener(actionListener);

	}
}
